package com.aiyige.page.pay.adapter;

import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.coupon.model.UserCoupon;
import com.aiyige.page.coupon.util.CouponUtil;
import com.aiyige.page.publish.CommonTipDialog;
import com.aiyige.utils.DateFormatUtil;
import com.aiyige.utils.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */
public class ChooseCouponAdapter extends BaseQuickAdapter<UserCoupon, ChooseCouponAdapter.ViewHolder> {


    public ChooseCouponAdapter() {
        super(R.layout.item_choose_coupon, new LinkedList<UserCoupon>());
    }

    @Override
    protected void convert(ViewHolder helper, UserCoupon item) {
        helper.bindData(item);
    }

    public class ViewHolder extends BaseViewHolder {


        @BindView(R.id.priceTvTag)
        TextView priceTvTag;
        @BindView(R.id.priceTv)
        TextView priceTv;
        @BindView(R.id.conditionTv)
        TextView conditionTv;
        @BindView(R.id.titleTv)
        TextView titleTv;
        @BindView(R.id.useTimeTv)
        TextView useTimeTv;
        @BindView(R.id.actionBtn)
        Button actionBtn;
        @BindView(R.id.couponUpLayout)
        LinearLayout couponUpLayout;
        @BindView(R.id.couponDivideLine)
        View couponDivideLine;
        @BindView(R.id.scopeTv)
        TextView scopeTv;
        @BindView(R.id.couponDownLayout)
        LinearLayout couponDownLayout;

        UserCoupon data;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }


        public void bindData(UserCoupon follow) {
            data = follow;
            titleTv.setText(data.getCouponName());
            try {
                String useableStartTimeStr = DateFormatUtil.formatMS("yyyy.M.dd", data.getUseableStartTime());
                String useableEndTimeStr = DateFormatUtil.formatMS("yyyy.M.dd", data.getUseableEndTime());
                useTimeTv.setText(useableStartTimeStr + "-" + useableEndTimeStr);
            } catch (Exception e) {
                useTimeTv.setText("");
            }
            try {
                priceTv.setText(StringUtils.priceFormat(data.getCouponAmount()));
            } catch (Exception e) {
                priceTv.setText("");
            }
            try {
                switch (data.getCouponType()) {
                    case CouponUtil.COUPON_TYPE_FULL_REDUCE:
                        conditionTv.setText(StringUtils.getString(R.string.full_reduce_condition, StringUtils.priceFormat(data.getFullAmount())));
                        break;
                    default:
                        conditionTv.setText("");
                        break;
                }
            } catch (Exception e) {
                conditionTv.setText("");
            }
            scopeTv.setText(data.getScopeDesc());
            try {
                actionBtn.setSelected(data.isSelected());
                if (data.getCurrentMomentAvailable().equals(CouponUtil.CURRENT_STATUS_AVAILABLE)) {
                    priceTvTag.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_brown_dba059));
                    priceTv.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_brown_dba059));
                    titleTv.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_black_111111));
                    conditionTv.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_black_111111));
                    actionBtn.setBackgroundResource(data.isSelected()?R.drawable.detail_selected:R.drawable.detail_normal);

                } else {
                    priceTvTag.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_gray_b1b5c1));
                    priceTv.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_gray_b1b5c1));
                    titleTv.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_gray_b1b5c1));
                    conditionTv.setTextColor(ContextCompat.getColor(mContext,R.color.zwcolor_gray_b1b5c1));
                    actionBtn.setBackgroundResource(R.drawable.detail_unavailable);
//                    actionBtn.setBackgroundResource(R.drawable.detail_unavailable);
//                    actionBtn.setEnabled(false);
                    actionBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            CommonTipDialog.newInstance(data.getCurrentMomentAvailableDesc())
                                    .show(((BaseActivity)mContext).getSupportFragmentManager(), "");
                        }
                    });
                }

            } catch (Exception e) {

            }

        }

    }


}
